{# form表单宏定义 #}

{% macro input(labeltext, name, value='', type='text', isneed=False, errormsg='',ext={}) -%}
<div class="control-group {{ ext.get('hide', '') }}" id="control-group-{{ name }}">
    <label class="control-label" id="control-label-{{ name }}">{{ labeltext|safe }}</label>
    <div class="controls" id="controls-{{ name }}">
        <input type="{{ type }}" name="{{ name }}" id="{{ name }}" value="{{ value }}">
        {% if isneed %}<span style="color: #ff4500">*</span>{% endif %}
        {% if ext.get('html', '') %}{{ ext.get('html') }}{% endif %}
        {% if errormsg %}<span class="label label-important">{{ errormsg }}</span>{% endif %}
        {% if ext.get('help-block', '') %}<span class="help-block">{{ ext.get('help-block')|safe }}</span>{% endif %}
        
    </div>
</div>
{%- endmacro %}

{% macro input2(labeltext, name,  value='1', type='text', min, max, placeholder, isneed=False, errormsg='',ext={}) -%}
<div class="control-group {{ ext.get('hide', '') }}" id="control-group-{{ name }}">
    <label class="control-label" id="control-label-{{ name }}">{{ labeltext|safe }}</label>
    <div class="controls" id="controls-{{ name }}">
        <input type="{{ type }}" name="{{ name }}" id="{{ name }}" min="{{ min }}" max="{{max}}" placeholder="{{placeholder}}" value="{{value}}">
        {% if isneed %}<span style="color: #ff4500">*</span>{% endif %}
        {% if ext.get('html', '') %}{{ ext.get('html') }}{% endif %}
        {% if errormsg %}<span class="label label-important">{{ errormsg }}</span>{% endif %}
        {% if ext.get('help-block', '') %}<span class="help-block">{{ ext.get('help-block')|safe }}</span>{% endif %}
        
    </div>
</div>
{%- endmacro %}

{% macro radio(labeltext, name, radio_group, default='', isneed=False, errormsg='', ext={}) %}
<div class="control-group {{ ext.get('hide', '') }}" id="control-group-{{ name }}">
    <label class="control-label" id="control-label-{{ name }}">{{ labeltext }}</label>
    <div class="controls" id="controls-{{ name }}">
    {% for item in radio_group %}
        <label class="radio"><input type="radio" name="{{ name }}" value="{{ item.value }}"
                {% if default==item.value %} checked="checked" {% endif %}>{{ item.name }}</label>
    {% endfor %}
    </div>
</div>
{%- endmacro %}

{% macro radio2(labeltext, name, radio_group, default='', isneed=False, errormsg='', ext={}) %}
<div class="control-group {{ ext.get('hide', '') }}" id="control-group-{{ name }}">
    <label class="control-label" id="control-label-{{ name }}">{{ labeltext }}</label>
    <div class="controls" id="controls-{{ name }}">
    {% for item in radio_group %}
        <label class="radio inline"><input type="radio" name="{{ name }}" value="{{ item.value }}"
                {% if default==item.value %} checked="checked" {% endif %}>{{ item.name }}</label>
    {% endfor %}
    </div>
</div>
{%- endmacro %}

{% macro checkbox(labeltext, name, checkbox_group, default='', isneed=False, errmsg='', ext={}) %}
<div class="control-group {{ ext.get('hide', '') }}" id="control-group-{{ name }}">
    {% set default = "%s" % default %}
    <label class="control-label" id="control-label-{{ name }}">{{ labeltext }}</label>
    <div class="controls" id="controls-{{ name }}">
        {% for item in checkbox_group %}
            {% set value = "%s" % item.value %}
            <label class="checkbox"><input type="checkbox" name="{{ name }}"
                value="{{ item.value }}"{% if default == value %} checked="checked"{% endif %}>{{ item.name }}</label>
        {% endfor %}
    </div>
</div>
{% endmacro %}

{% macro submit(btntext='提交', errormsg='') -%}
    <div class="control-group">
        <div class="controls">
            <button type="submit" class="btn btn-success" style="min-width: 80px;" id="submit">{{ btntext }}</button>
            {% if errormsg %}<span class="label label-important">{{ errormsg }}</span>{% endif %}
        </div>
    </div>
{%- endmacro %}

{% macro select(labeltext, name, options, default='', isneed=False, errormsg='', ext={}) -%}
<div class="control-group {{ ext.get('hide', '') }}" id="control-group-{{ name }}">
    <label class="control-label" id="control-label-{{ name }}">{{ labeltext }}</label>
    <div class="controls" id="controls-{{ name }}">
        <select size="1" name="{{ name }}" id="{{ name }}">
        {% for item in options %}
            {% if item.value == default %}
                <option value="{{ item.value }}" selected="selected">{{ item.name }}</option>
            {% else %}
                <option value="{{ item.value }}">{{ item.name }}</option>
            {% endif %}
        {% endfor %}
        </select>
        {% if isneed %}<span style="color: #ff4500">*</span>{% endif %}
        {% if errormsg %}<span class="label label-important">{{ errormsg }}</span>{% endif %}
    </div>
</div>
{%- endmacro %}


{% macro select3(labeltext, name, options, default='', isneed=False, errormsg='',ext={}) -%}
<div class="control-group {{ ext.get('hide', '') }}" id="control-group-{{ name }}">
    <label class="control-label" id="control-label-{{ name }}">{{ labeltext }}</label>
    <div class="controls" id="controls-{{ name }}" >
        <select size="1" name="{{ name }}" id="{{ name }}">
        <option></option>
        {% for item in options %}
            {% if item.cat_id == default %}
                
                <option value="{{ item.cat_id }}" selected="selected">{{ item.cat_name }}</option>
            {% else %}
                <option value="{{ item.cat_id }}">{{ item.cat_name }}</option>
            {% endif %}
        {% endfor %}
        </select>
        {% if isneed %}<span style="color: #ff4500">*</span>{% endif %}
        {% if errormsg %}<span class="label label-important">{{ errormsg }}</span>{% endif %}
    </div>
</div>
{%- endmacro %}


{% macro textarea(labeltext, name, value='', min_width='200px', min_height='80px', isneed=False,  errormsg='', ext={}) -%}
<div class="control-group {{ ext.get('hide', '') }}" id="control-group-{{ name }}">
    <label class="control-label" id="control-label-{{ name }}">{{ labeltext }}</label>
    <div class="controls" id="controls-{{ name }}">
        <textarea id="{{ name }}" name="{{ name }}" style="min-width: {{ min_width }}px; min-height: {{ min_height }}px;">{{ value }}</textarea>
        {% if isneed %}<span style="color: #ff4500">*</span>{% endif %}
        {% if ext.get('html', '') %}{{ ext.get('html')|safe }}{% endif %}
        {% if errormsg %}<span class="label label-important">{{ errormsg }}</span>{% endif %}
    </div>
</div>
{%- endmacro %}

{% macro datepicker(labeltext, name, value='', format='yyyy-mm-dd', isneed=False,  errormsg='', ext={}) -%}
<div class="control-group {{ ext.get('hide', '') }}" id="control-group-{{ name }}">
    <label class="control-label" id="control-label-{{ name }}">{{ labeltext }}</label>
    <div class="controls" id="controls-{{ name }}">
        <input class="datepicker" id="{{ name }}" name="{{ name }}" value="{{ value }}" type="text" data-date-format="{{ format }}" >
        {% if isneed %}<span style="color: #ff4500">*</span>{% endif %}
        {% if errormsg %}<span class="label label-important">{{ errormsg }}</span>{% endif %}
    </div>
</div>
{%- endmacro %}

{% macro get_img_view_html(img_url) -%}
{% if img_url %}<a target="_blank" href="{{ img_url }}">查看</a>{% endif %}
{%- endmacro %}

{% macro pagination_html(pagination, endpoint, query_string) -%}
{% if pagination.total > 0 %}
<div class="pagination pagination-centered">
    <ul>
        <li class="disabled"><a href="javascript:void(0);">总共{{ pagination.total }}条/{{ pagination.pages }}页</a></li>
        {% if pagination.has_prev %}<li><a href="{{ pagination_href(endpoint, pagination.prev_num, query_string) }}">上一页</a></li>{% endif %}
    {% for page in pagination.iter_pages() %}
        {% if page %}
            {% if page != pagination.page %}
                <li><a href="{{ pagination_href(endpoint, page, query_string) }}">{{ page }}</a></li>
            {% else %}
                <li class="active"><a href="javascript:void(0);">{{ page }}</a></li>
            {% endif %}
        {% else %}
            <li class="disabled"><a href="javascript:void(0);">…</a></li>
        {% endif %}
    {% endfor %}
        {% if pagination.has_next %}<li><a href="{{ pagination_href(endpoint, pagination.next_num, query_string) }}">下一页</a></li>{% endif %}
    </ul>
</div>
{% endif %}
{%- endmacro %}

{% macro pagination_href(endpoint, page, query_string) -%}
{{ url_for(endpoint, page=page) }}{% if query_string %}{{ '?' + query_string }}{% endif %}
{%- endmacro %}

{% macro have_data(data_list, colspan) -%}
    {% if not data_list %}
        <tr><td colspan="{{ colspan }}" style="text-align: center;">暂无数据</td></tr>
    {% endif %}
{%- endmacro %}

{% macro format_longi_lati(longitude, latitude) -%}
{% set longitude_latitude = '' %}
{% if longitude and latitude -%}
    {{longitude}},{{latitude}}
{%- endif %}
{%- endmacro %}

{% macro get_title(request, module_name) -%}
{% if request.base_url.find('edit') != -1 %}编辑{{module_name}}{% else %}添加新{{module_name}}{% endif %}
{%- endmacro %}

